package com.supermap.dataservice.common;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.common.dao.YieldDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.common.YieldService;
import com.supermap.dubbo.api.common.entity.Crop;
import com.supermap.dubbo.api.common.entity.Yield;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.Year;
import java.util.Calendar;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:产量业务逻辑层
 * @Date:18/8/22 下午2:53
 */
@Service
public class YieldServiceImpl implements YieldService{

    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(YieldServiceImpl.class);

    @Autowired
    private YieldDao yieldDao;

    @Override
    public JSONUtil getYieldsByCropAndTime(String cropCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        int time = Calendar.getInstance().get(Calendar.YEAR);
            //默认为6年以内的产量信息
        int num = 6;
        try {
            for(int i=0;i<=num;i++){
                //给没有的产量赋值为0
                Yield yield = new Yield();
                yield.setCrop(cropCode);
                yield.setYield(0);
                yield.setTime(time);
                List<Yield> yields = yieldDao.checkFieldExits(yield);
                if(yields==null || yields.size()==0) {
                    yieldDao.addYieldInfo(yield);
                }
                time--;
            }
            //获取时间段内的作物产量信息
            List<Yield> yields = yieldDao.getYieldsInfoByCropAndTime(cropCode,++time);

            //获取时间段内的作物产量信息后返回的json数据
            jsonUtil.setMsg("get yields success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(yields);
        }catch (Exception e) {
            //捕捉数据库异常
            e.printStackTrace();
            logger.error("YieldServiceImpl=>getYieldsByCropAndTime=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("get yields flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateYieldsInfo(List<Yield> yields) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            //循环更新产量信息
            for (Yield y:yields) {
                yieldDao.updateYieldsInfo(y);
            }
            //循环更新产量信息后返回的json数据
            jsonUtil.setMsg("update yields success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(yields);
        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("YieldServiceImpl=>updateYieldsInfo=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("update yields flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil addYieldInfo(Yield yield) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Yield> yields = yieldDao.checkFieldExits(yield);
            if(yields==null || yields.size()==0){
                //增加产量信息
                yieldDao.addYieldInfo(yield);
                //增加产量信息后返回的json数据
                jsonUtil.setMsg("add yield success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(yield);
            }else{
                //增加产量信息后返回的json数据
                jsonUtil.setMsg("add yield false,yield has exit ！");
                jsonUtil.setIsSuccess(false);
                jsonUtil.setCode(100);
                jsonUtil.setData(yield);
            }



        }catch (Exception e) {
            //捕捉数据库异常
            logger.error("YieldServiceImpl=>addYieldInfo=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("add yield flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
}
